package com.mach.platform.repository;

import com.mach.platform.domain.base.Menu;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Anonymous
 * @Description: 组织架构 数据库
 * @Package: com.mach.platform.com.mach.platform.repository
 * @time 2018/04/12 17:44
 */

@Repository
public interface MenuRepo extends BaseRepository<Menu> {

	/**
	 * 获取 所有菜单 对应的角色
	 * @param accountId
	 * @return
	 */
	@Query(value = "select *\n" +
			"from menu m where m.id in (select distinct rm.menu_id\n" +
			"from account_role ar join role_menu rm on ar.role_id= rm.role_id where ar.account_id=?1) and front_static = 0",nativeQuery = true)
	List<Menu> loadMenuWithInUserRolds(Long accountId);


	/**
	 * 查询 某个 菜单的 所有 子孙菜单id， 包括自己
	 * @param id
	 * @return
	 */
	@Query(value = "select countupMenuSubsets(?1) ", nativeQuery = true)
	String loadMenuSubsetsById(Long id);

	/**
	 * 删除 菜单 角色 关系
	 * @param ids
	 * @return
	 */
	@Modifying
	@Query(value = "delete from role_menu where menu_id in (?1)", nativeQuery = true)
	int delRelationByMenuIds(List<Long> ids);


    /**
     * 根据id 获取对应的菜单数据
     * @param id
     * @return
     */
	@Query(value = "select * from menu where id in (select distinct menu_id from role_menu where role_id = ?1)", nativeQuery = true)
    List<Map> loadMenusByRoleId(Long id);
    /**
     *  获取扥请求用户对应的菜单数据
     * @param userId
     * @return
     */
	@Query(value = "select distinct m.* from menu m left join role_menu rm on rm.menu_id = m.id left join role_account ra on ra.role_id = rm.role_id where ra.account_id =?1", nativeQuery = true)
    List<Menu> loadUserAuthMenus(Long userId);

    /**
     *  获取扥请求用户对应的菜单数据
     * @param rootUrl
     * @return
     */
    @Query(value = "select selectSumMenuByName(?1) ", nativeQuery = true)
    String loadUserAuthMenusForFuns(String rootUrl);

    Menu findByUrl(String rootUrl);
}
